package com.solution.admin.customer.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.CustomerVisitLogEntity;
import com.solution.admin.customer.service.CustomerVisitLogService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 客户访问内容web控制层
 * @author lilp
 * @date 2018-07-07 09:26:31
 */
@RequestMapping("/customerVisitLog")
@Controller
public class CustomerVisitLogController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CustomerVisitLogController.class);

	@Autowired
	private CustomerVisitLogService customerVisitLogService;
	
	/**
	 * 跳转客户访问内容查询页面
	 * @param CustomerVisitLog 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2018-07-07 09:26:31
	 */
	@RequestMapping("/toListCustomerVisitLogPage")
	public String toListCustomerVisitLogPage(CustomerVisitLogEntity customerVisitLog, HttpServletRequest request) {
		return "customer/customerVisitLog_list";
	}

	/**
	 * 分页查询客户访问内容列表
	 * @param customerVisitLog 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2018-07-07 09:26:31
	 */
	@RequestMapping("/listCustomerVisitLogPage")
	@ResponseBody
	public JSONGrid listCustomerVisitLogPage(CustomerVisitLogEntity customerVisitLog, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		// 公司ID
		String companyId = request.getParameter("companyId");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("companyId", companyId);
		try {
			PageBean pageBean = customerVisitLogService.listCustomerVisitLogPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询客户访问内容失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2018-07-07 09:26:31
	 */
	@RequestMapping("/toAddCustomerVisitLog")
	public String toAddCustomerVisitLog(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/customerVisitLog_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2018-07-07 09:26:31
	 */
	@RequestMapping("/toUpdateCustomerVisitLog")
	public String toUpdateCustomerVisitLog(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CustomerVisitLogEntity customerVisitLog = customerVisitLogService.getCustomerVisitLogById(id);
			request.setAttribute("customerVisitLog", customerVisitLog);
		} catch (Exception e) {
			handleException("跳转编辑客户访问内容页面错误", logger, e);
		}
		return "customer/customerVisitLog_form";
	}


	/**
	 * 新增或保存客户访问内容
	 * @param request 请求对象
	 * @param customerVisitLogEntity 客户访问内容实体
	 * @return
	 * @author lilp
 	 * @date 2018-07-07 09:26:31
	 */
	@RequestMapping("/saveOrUpdateCustomerVisitLog")
	@ResponseBody
	public Map<String, Object> saveCustomerVisitLog(HttpServletRequest request, CustomerVisitLogEntity customerVisitLogEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = customerVisitLogService.updateCustomerVisitLog(customerVisitLogEntity);
			}else{
				result = (int)customerVisitLogService.saveCustomerVisitLog(customerVisitLogEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加客户访问内容失败", logger, e);
		}
		return resultMap;
	}
	
}
